<!doctype html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-model="http://www.w3.org/1999/xhtml" xmlns:v-demo="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .router-link-active {
            color:#f00;
        }
        pre {
            font-family: sans-serif;
            font-size: 1em;
            line-height:1.5em;
        }

    </style>
</head>
<body>
<pre>
    beforeRouteEnter 中的this指向Window而不是Vue实例
    可能通过next参数的回调函数的参数访问Vue实例
</pre>

<div id="app">
    <ul>
        <li>
            <router-link to="/a">/a</router-link>
        </li>
        <li>
            <router-link to="/b">/b</router-link>
        </li>
    </ul>
    <router-view></router-view>
</div>
</body>

<script src="../../../vender/vue@2.4.2.js"></script>
<script src="../../../vender/vue-router@2.7.0.js"></script>

<script>
    const A={template:'<div>A.</div>'};
    const B={
        template:`<div>
            Route:B<br>
            <router-link to="/b/1">/b/1</router-link>
            <router-link to="/b/2">/b/2</router-link>
            <router-view></router-view>
        </div>`,
        beforeRouteEnter(to,from,next){
        console.info('beforeRouteEnter:',this);//Window
        next(vm=>{
            console.log('vm:',vm);
        });
    },
    beforeRouteUpdate(to,from,next){
        console.log('beforeRouteUpdate:',this);
        next();
    },
    beforeRouteLeave(to,from,next){
        console.log('beforeRouteLeave:',this);
        next();
    }
    };

    const router=new VueRouter({
        routes:[
            {path:'/a',component:A},
            {
                path:'/b',component:B,
                children:[
                    {
                        path:':id',
                        component:{
                            template:`<div>id:{{$route.params.id}}</div>`
                        }
                    }
                ]

            }
        ]
    });


    const app=new Vue({
        el:'#app',
        router
    });

</script>
</html>